package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zcf
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private SetmealDishMapper setmealDishMapper;

    @Resource
    private DishMapper dishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @CacheEvict(value = "setmeal:cache",key="#setmealDTO.categoryId")
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //新增套餐
        setmealMapper.insert(setmeal);

        //保存套餐和菜品之间的关系
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
        }
        //插入中间表
        setmealDishMapper.insertBatch(setmealDishes);
    }


    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //1.设置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        //2.查询集合
        List<Setmeal> setmealList = setmealMapper.pageQuery(setmealPageQueryDTO);
        Page<Setmeal> setmealList1 = (Page<Setmeal>) setmealList;
        //封装结果集
        return new PageResult(setmealList1.getTotal(), setmealList1.getResult());
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "setmeal:cache",allEntries = true)
    public void delete(List<Long> ids) {
        //1.查看套餐是否是起售中
        ids.forEach(
                id -> {
                    Setmeal setmeal = setmealMapper.getById(id);
                    if (setmeal.getStatus().equals(StatusConstant.ENABLE)) {
                        throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
                    }
                }
        );

        //2.1删除套餐
        setmealMapper.deleteByIds(ids);
        //2.2删除套餐和菜品的关联关系
        setmealDishMapper.deleteBySetmealIds(ids);
    }


    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = setmealMapper.getByDishID(id);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "setmeal:cache",allEntries = true)
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //更新套餐
        setmealMapper.update(setmeal);

        //保存套餐和菜品之间的关系
        //1.先删除套餐和菜品的关联关系
        List<Long> list = new ArrayList<>();
        list.add(setmealDTO.getId());
        setmealDishMapper.deleteBySetmealIds(list);
        //2.保存套餐和菜品的关联关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });
        }
        setmealDishMapper.insertBatch(setmealDishes);
    }


    //修改套餐起售停售
    @CacheEvict(value = "setmeal:cache",allEntries = true)
    @Override
    public void updateStatus(Integer status, Long id) {
        //1.起售套餐时判断里面是否有停售菜品
        if (status.equals(StatusConstant.ENABLE)) {
            //根据套餐id查询菜品
            List<Dish> list = dishMapper.getBySetmealId(id);
            //遍历菜品
            if(list != null && list.size() > 0){
                list.forEach(
                        dish -> {
                            if (dish.getStatus().equals(StatusConstant.DISABLE)) {
                                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                            }
                        }
                );
            }
        }
        Setmeal build = Setmeal.builder().id(id).status(status).build();
        setmealMapper.update(build);
    }


}
